package com.best.bestadmin.controller;

import com.best.bestadmin.pojo.AdminUser;
import com.best.bestadmin.service.AdminService;
import com.best.pojo.AppUser;
import com.best.pojo.Food;
import com.best.pojo.FoodComment;
import com.best.pojo.UserLoginRequest;
import com.best.result.Response;
import com.best.result.ResultCodeEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台管理
 *
 * @author yangruyi
 * @date 2021/3/14
 */
@RestController
@RequestMapping("/admin/")
public class AdminController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private AdminService adminService;
    /**
     * 用户列表
     *
     * @param name 名字，模糊搜索
     * @param state 状态，是否在黑名单 0 否 ,1 是
     * @return List
     */
    @GetMapping("users")
    public List<AppUser> users(@RequestParam(required = false) String name, @RequestParam Integer state){
        return adminService.checkUsers(name,state);
    }

    /**
     * 黑名单操作
     *
     * @param userId 用户id
     * @param state 状态，是否在黑名单 0 否 ,1 是
     * @return Response
     */
    @PutMapping("user/{userId}/{state}")
    public Response hate(@PathVariable String userId,@PathVariable Integer state){
        boolean b = adminService.hateUser(userId, state);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return Response
     */
    @DeleteMapping("user/{id}")
    public Response del(@PathVariable String id){
        boolean b = adminService.delUser(id);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 作品列表
     *
     * @return List
     */
    @GetMapping("foods")
    public List<Food> foods(){
        // 审核不通过
        int state = 0;
        return adminService.checkFoods(state);
    }

    /**
     * 作品审核
     *
     * @param foodId 作品id
     * @param state 状态 0 未审核 1 审核通过 2 审核不通过
     * @return Response
     */
    @PutMapping("food/{foodId}/{state}")
    public Response audit(@PathVariable String foodId, @PathVariable Integer state){
        boolean b = adminService.auditFood(foodId, state);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 删除作品
     *
     * @param foodId 作品id
     * @return Response
     */
    @DeleteMapping("food/{foodId}")
    public Response delFood(@PathVariable String foodId){
        boolean b = adminService.delFood(foodId);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 评论列表
     *
     * @param words 模糊搜索某些评论
     * @return List
     */
    @GetMapping("comment")
    public List<FoodComment> comments(@RequestParam(required = false) String words){
        return adminService.checkComments(words);
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     * @return Response
     */
    @DeleteMapping("comment/{id}")
    public Response delComment(@PathVariable String id){
        boolean b = adminService.delComment(id);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 登录
     *
     * @param request UserLoginRequest
     * @return Response
     */
    @PostMapping("/login")
    public Response login(@RequestBody @Validated UserLoginRequest request){
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(request.getUsername(),request.getPassword()));
            return Response.ok().data("user",adminService.authentication(request.getUsername()));
        } catch (UnknownAccountException e) {
            logger.info("msg:{}","用户名错误");
            return Response.setResult(ResultCodeEnum.USERNAME_ERROR);
        }catch (IncorrectCredentialsException e){
            logger.info("msg:{}","密码错误");
            return Response.setResult(ResultCodeEnum.PASSWORD_ERROR);
        }
    }

    /**
     * 修改名字/密码
     *
     * @param adminUser AdminUser
     * @return Response
     */
    @PutMapping("/admin")
    public Response editUser(@RequestBody @Validated AdminUser adminUser){
        boolean b = adminService.editAdmin(adminUser);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }
}
